package com.jnc.web.biz.system.controller;

import com.jnc.common.base.BaseResp;
import com.jnc.core.base.controller.BaseController;
import com.jnc.core.web.page.TableResult;
import com.jnc.web.biz.system.model.DictData;
import com.jnc.web.biz.system.service.DictDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: jjn
 * @Date: 2018/11/7
 * @Desc:
 */
@Slf4j
@Controller
@RequestMapping("/system/dictData")
public class DictDataController extends BaseController {
    private String prefix = "system/dictData";

    @Autowired
    private DictDataService dictDataService;

    /**
     * 初始化展示页面
     * @return
     */
    @RequiresPermissions("system:dict:view")
    @GetMapping()
    public String init(){
        return prefix + "/list";
    }

    /**
     * 初始化新增页面
     * @param model
     * @return
     */
    @GetMapping("/addInit/{dictType}")
    public String addInit(@PathVariable("dictType") String dictType, ModelMap model){
        model.put("dictType", dictType);
        return prefix + "/add";
    }

    /**
     * 初始化编辑页面
     * @param dataId
     * @param model
     * @return
     */
    @GetMapping("/editInit/{dataId}")
    public String editInit(@PathVariable("dataId") Integer dataId, ModelMap model){
        model.put("info", dictDataService.getDictData(dataId));
        return prefix + "/edit";
    }

    /**
     * 查询列表数据
     * @param dictData
     * @return
     */
    @RequiresPermissions("system:dict:list")
    @PostMapping("/list")
    @ResponseBody
    public TableResult list(DictData dictData){
        log.debug("DictDataController.list.dictData : {}", dictData);
        initPage();
        List<DictData> list = dictDataService.queryDictData(dictData);
        return getTableData(list);
    }

    /**
     * 新增
     * @param dictData
     * @return
     */
    @RequiresPermissions("system:dict:add")
    @PostMapping("/add")
    @ResponseBody
    public BaseResp add(@RequestBody DictData dictData){
        log.debug("DictDataController.add.dictData : {}", dictData);
        //校验同一字典类型的字典编码是否已存在
        List<DictData> list = dictDataService.checkDictData(dictData);
        if(list != null && list.size() > 0){
            return error(String.format("该字典类型[%s]的字典编码[%s]已经存在", dictData.getDictType(), dictData.getDictCode()));
        }
        return toAjax(dictDataService.addDictData(dictData));
    }

    /**
     * 编辑
     * @param dictData
     * @return
     */
    @RequiresPermissions("system:dict:edit")
    @PostMapping("/edit")
    @ResponseBody
    public BaseResp edit(@RequestBody DictData dictData){
        log.debug("DictDataController.edit.dictData : {}", dictData);
        //校验同一字典类型的字典编码是否已存在
        List<DictData> list = dictDataService.checkDictData(dictData);
        if(list != null && list.size() > 0){
            return error(String.format("该字典类型[%s]的字典编码[%s]已经存在", dictData.getDictType(), dictData.getDictCode()));
        }
        return toAjax(dictDataService.editDictData(dictData));
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @RequiresPermissions("system:dict:remove")
    @PostMapping("/remove")
    @ResponseBody
    public BaseResp remove(@RequestParam String ids){
        log.debug("DictDataController.delete.ids : {}", ids);
        return toAjax(dictDataService.deleteDictData(ids));
    }
}
